﻿namespace 异常处理
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");


            int[] marr = { 1, 2, 3, 4 };

            try
            {
                // 可能发生异常的语句放这里
                int a = marr[4];

                Console.WriteLine("上面的语句发生了异常，这里的语句就不会再执行了");
            }
            catch (NullReferenceException  ex) // System.IndexOutOfRangeException: Index was outside the bounds of the array.
            {
                // 处理异常，捕获异常
                Console.WriteLine ("空指针异常");

            }
            catch(IndexOutOfRangeException ex)
            {
                // 处理异常，捕获异常
                
                Console.WriteLine ("下标越界异常");
                Console.WriteLine (ex.StackTrace);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally // 始终执行
            {
                Console.WriteLine(" finally ");

            }

            Console.WriteLine("异常被处理了，这里的语句是会被执行的");

        }
    }
}
